package com.koi.generator.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.ex.koi.YUtil;
import com.koi.generator.service.gencloumn.GenColumnService;
import com.koi.model.bo.element.SelectStringBo;
import com.koi.model.vo.gen.GenColumnVo;
import com.koi.pojo.gen.GenColumn;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author ･ᴗ･
 * @description [代码生成字段表-控制层]
 */
@Validated
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class GenColumnController {

    private final GenColumnService genColumnService;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @GetMapping("/genColumn/listPage")
    public IPage<GenColumn> listPage(GenColumnVo vo) {
        // 分页构造器
        Page<GenColumn> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<GenColumn> wrapper = new LambdaQueryWrapper<>();
        // 进行分页查询
        page = genColumnService.page(page, wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/genColumn/list")
    public List<GenColumn> list() {
        return genColumnService.list();
    }

    /**
     * @description [查询一个]
     * @author ･ᴗ･
     */
    @GetMapping("/genColumn/getById/{id}")
    public GenColumn getById(@PathVariable Long id) {
        return genColumnService.getById(id);
    }

    /**
     * @description [新增]
     * @author ･ᴗ･
     */
    @PostMapping("/genColumn/add")
    public void add(@Validated @RequestBody GenColumn genColumn) {
        YUtil.isTrue(!genColumnService.save(genColumn), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ･ᴗ･
     */
    @PostMapping("/genColumn/update")
    public void update(@Validated @RequestBody GenColumn genColumn) {
        YUtil.isTrue(!genColumnService.updateById(genColumn), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ･ᴗ･
     */
    @PostMapping("/genColumn/deleteById/{id}")
    public void delete(@PathVariable Long id) {
        YUtil.isTrue(!genColumnService.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     * @author ･ᴗ･
     */
    @PostMapping("/genColumn/batchDelete")
    public void batchDelete(@RequestBody List<Long> ids) {
        YUtil.isTrue(!genColumnService.removeBatchByIds(ids), "删除失败，请稍后重试");
    }

    /**
     * @description [根据表名查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/genColumn/listColumn/{tableSchema}/{tableName}")
    public List<GenColumn> listColumn(@PathVariable("tableSchema") String tableSchema, @PathVariable("tableName") String tableName) {
        LambdaQueryWrapper<GenColumn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GenColumn::getTableSchema, tableSchema);
        wrapper.eq(GenColumn::getTableName, tableName);
        return genColumnService.list(wrapper);
    }

    /**
     * @description [根据表名查询树表选择字段下拉框]
     * @author ･ᴗ･
     */
    @GetMapping("/genColumn/listElSelectColumn/{tableSchema}/{tableName}")
    public List<SelectStringBo> listElSelectColumn(@PathVariable("tableSchema") String tableSchema, @PathVariable("tableName") String tableName) {
        LambdaQueryWrapper<GenColumn> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(GenColumn::getJavaField, GenColumn::getColumnName);
        wrapper.eq(GenColumn::getTableSchema, tableSchema);
        wrapper.eq(GenColumn::getTableName, tableName);
        List<GenColumn> genColumnList = genColumnService.list(wrapper);
        if(CollectionUtil.isNotEmpty(genColumnList)){
            List<SelectStringBo> selectStringBoList = new ArrayList<>();
            SelectStringBo selectStringBo = null;
            for (GenColumn genColumn : genColumnList) {
                selectStringBo = new SelectStringBo();
                selectStringBo.setLabel(genColumn.getJavaField());
                selectStringBo.setValue(genColumn.getColumnName());
                selectStringBoList.add(selectStringBo);
            }
            return selectStringBoList;
        }
        return Collections.emptyList();
    }

}
